var readline = require('readline-sync')
readline.setDefaultOptions({ prompt: '' })
var readline = readline.prompt
let [n, k] = readline().split(' ').map((item) => parseInt(item)) // 转换成数字
let nameList = []
for (let i = 0; i < n; i++) {
  nameList.push(i)
}
function hotPotato (nameList, num) {
  let arr = [...nameList],
    eliminated = ''
  while (arr.length > 1) {
    for (let i = 0; i < num; i++) {
      arr.push(arr.shift())
    }
    eliminated = arr.shift()
  }
  console.log(arr[0])
}
hotPotato(nameList, k)
// let names = [' John', ' Jack', ' Camila', ' Ingrid', ' Carl']; hotPotato(names, 7)